/*
 * Copyright Radosław Smogura, 2012. All rights reserved.
 *
 * The content of this file is confidential and it is intelectual property
 * of copyright owners.
 * 
 *
 * $URL: http://reprap-ide.googlecode.com/svn/trunk/RepRapIDE/RepRapProject/src/main/java/eu/softper/reprap/generator/GeneratorFactory.java $
 * $Id: GeneratorFactory.java 3 2012-11-19 10:58:30Z mail@smogura.eu $
 * $Revision: 3 $
 */
package eu.softper.reprap.generator;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 *
 * @author rsmogura
 */
public class GeneratorFactory<T extends Number & Comparable<T>> {
    private NumericFactory<T> numericFactory;
    private int numberOfThreads;
    private GenerationContextImpl ctx;

    private SceneHandler<T> sceneHandler;
    private List<SceneTriangle<T>> orderedTrinagles;
    
    public GeneratorFactory(NumericFactory<T> numericFactory, int numberOfThreads, GenerationContextImpl ctx) {
        this.numericFactory = numericFactory;
        this.numberOfThreads = numberOfThreads;
        this.ctx = ctx;
    }
    
    public SceneHandler<T> getHandler() {
        if (sceneHandler == null)
            sceneHandler = new SceneHandler<T>(numericFactory, ctx);
        
        return sceneHandler;
    }

    public List<SceneTriangle<T>> getOrderedTrinagles() {
        if (orderedTrinagles == null) {
            orderedTrinagles = new ArrayList<SceneTriangle<T>>(sceneHandler.getTriangles());
            Collections.sort(orderedTrinagles, new SceneTriangle.LowerHeightComparator());
        }
        
        return orderedTrinagles;
    }
    
    public MultiThreadSlicerImpl<T> createSlicer() {
        return new MultiThreadSlicerImpl<T>(numericFactory, numberOfThreads, BigDecimal.ZERO, ctx.getLayerHeight(), getOrderedTrinagles(), ctx);
    }
    
    public static <Y extends Number & Comparable<Y>> GeneratorFactory<Y> createFactory(NumericFactory<Y> numericFactory, int numberOfThreads, GenerationContextImpl ctx) {
        return new GeneratorFactory(numericFactory, numberOfThreads, ctx);
    }
}
